<?php
declare(strict_types= 1);
namespace app\admin\service;
use app\admin\model\ArcIndex;
use app\admin\model\ArcModel;
use app\admin\model\Category;
use app\admin\service\interface\ArticleService;
use think\facade\Db;

class ArticleServiceImpl implements ArticleService
{

    public function add(int $user_id,int $category_id,string $title,string $thumb_path,array $data = []){
        //发布文档索引信息
        //根据文档模型填充文档详情

        $category = Category::findOrEmpty($category_id); 
        if($category->isEmpty()){
            throw new \Exception("栏目不存在！");
        }
        // $model = Category
        Db::startTrans();
        try {
            $index = new ArcIndex();
            $index->user_id = $user_id;
            $index->category_id = $category_id;
            $index->title = $title;
            $index->thumb_path = $thumb_path;
            $index->save();
            /**
             * @var ArcModel
             */
            $model = $category->model;

            $table_name = ArcModel::TABLE_PREFIX .$model->table;
            $data['arc_id']=$index->id;
            Db::name($table_name)->insert($data);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
        

        
        
    }
    public function edit(int $user_id,int $arc_id,int $category_id,string $title,string $thumb_path,array $data = []){
        $category = Category::findOrEmpty($category_id); 
        if($category->isEmpty()){
            throw new \Exception("栏目不存在！");
        }
        $index = ArcIndex::findOrEmpty($arc_id);
        if($index->isEmpty()){
            throw new \Exception("文档不存在！");
        }
        $category_pre = $index->category;
        if($category->model_id == $category_pre->model_id){
            Db::startTrans();
            try {
      
                $index->user_id = $user_id;
                $index->category_id = $category_id;
                $index->title = $title;
                $index->thumb_path = $thumb_path;
                $index->save();
                /**
                 * @var ArcModel
                 */
                $model = $category->model;
    
                $table_name = ArcModel::TABLE_PREFIX .$model->table;
                // $data['arc_id']=$index->id;
                Db::name($table_name)->where('arc_id',$arc_id)->update($data);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                throw $e;
            }
        }else{
            Db::startTrans();
            try {
                // 删除旧数据
                $table_name_pre = ArcModel::TABLE_PREFIX .$category_pre->model->table;
                Db::name($table_name_pre)->where('arc_id',$arc_id)->delete();

                $index->user_id = $user_id;
                $index->category_id = $category_id;
                $index->title = $title;
                $index->thumb_path = $thumb_path;
                $index->save();
                /**
                 * @var ArcModel
                 */
                $model = $category->model;
                $table_name = ArcModel::TABLE_PREFIX .$model->table;
                $data['arc_id']=$index->id;
                Db::name($table_name)->insert($data);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                throw $e;
            }
        }

    }
    public function check(int $arc_id,int $status){

    }

    public function delete(int $arc_id){
        $index = ArcIndex::findOrEmpty($arc_id);
        if($index->isEmpty()){
            throw new \Exception("文档不存在！");
        }
        $category = $index->category;
        $model = $category->model;
        $table_name = ArcModel::TABLE_PREFIX .$model->table;
        Db::startTrans();
        try {

            Db::name($table_name)->where('arc_id',$arc_id)->delete();
            $index->delete();
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function search(){
        
    }
}
